#!/bin/bash

DOWN=${DOWN:-https://mirrors.ustc.edu.cn/rust-static/rustup/dist/x86_64-unknown-linux-gnu/rustup-init}

if [[ -d $HOME/.cargo ]]; then echo "rust is installed"; exit 0; fi

SUDO="sudo "
if [[ $(id -u) -eq 0 ]]; then SUDO=""; fi

wget $DOWN
chmod a+x rustup-init
RUSTUP_DIST_SERVER=https://mirrors.tuna.tsinghua.edu.cn/rustup ./rustup-init -y
echo "RUSTUP_DIST_SERVER=https://mirrors.tuna.tsinghua.edu.cn/rustup"  >> $HOME/.cargo/env

cat << "EOF" | tee $HOME/.cargo/config
[source.crates-io]
replace-with = 'rsproxy'

[source.rsproxy]
registry = "https://rsproxy.cn/crates.io-index"

[registries.rsproxy]
index = "https://rsproxy.cn/crates.io-index"

[net]
git-fetch-with-cli = true
EOF

